<?php


namespace app\admin\controller\question;


use app\common\controller\Adminbase;
use think\Db;

class Questionfback extends Adminbase
{

    //首页
    //查看问题
    //反馈问题
    public function index(){
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            //获取列表
            $where[]=['qf.is_del','=',0];
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['nickname'])) {
                    $where[] = ['m.nickname', 'like', '%'.$filter['nickname'].'%'];
                }
                if (isset($filter['is_reply'])) {
                    $where[] = ['qf.is_reply', '=',  $filter['is_reply']];
                }
                if (isset($filter['create_time'])) {
                    $create_time = explode(' - ', $filter['create_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['qf.create_time', 'between', [$create_time[0], $create_time[1]]];
                }
            }
            $_list = Db::name('question_feedback')->alias('qf')
                ->join('member m','qf.member_id=m.id')
                ->field('qf.*,m.nickname,m.mobile')
                ->where($where)
                ->page($page, $limit)
                ->order('create_time desc')
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
            }
            //获取数量
            $total = Db::name('question_feedback')->alias('qf')
                ->join('member m','qf.member_id=m.id')   //0待审核 3审核失败
                ->where($where)
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        return $this->fetch();
    }
    //详情
    public function edit(){
        $id = $this->request->param('id/d', 0);
        $detail=Db::name('question_feedback')->find($id);
        $detail_img=Db::name('question_feedback_img')
            ->where('mf_id',$id)
            ->column('img_url');
        $detail['create_time']=date('Y-m-d H:i:s',$detail['create_time']);
        $detail['img_list']=$detail_img;
        $this->assign('data',$detail);
        return $this->fetch();

    }

    //反馈
    public function reply(){
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            $update_data = array(
                'id' => $data['id'],
                'is_reply' => 1,
                'reply_content' => $data['reply_content'],
            );
            $stmt = Db::name('question_feedback')->update($update_data);
            sendMessage($data['member_id'], '您反馈的问题:'.$data['content'].' //系统反馈:'.$data['reply_content'], 1, 3, 0);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        }
        $id = $this->request->param('id/d', 0);
        $detail=Db::name('question_feedback')->find($id);
        $detail_img=Db::name('question_feedback_img')
            ->where('mf_id',$id)
            ->column('img_url');
        $detail['create_time']=date('Y-m-d H:i:s',$detail['create_time']);
        $detail['img_list']=$detail_img;
        $this->assign('data',$detail);
        return $this->fetch();

    }
}